package com.andnnl.bytes;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Created by chenss on 2020/7/31.
 */
public class BytesTest {


    /**
     * 功能：
     * 将字节转换为16进制码（在此只是为了调试输出，此函数没有实际意义）
     *
     * @param b
     * @return 转化后的16进制码
     * @Author: eric(eric_cheung709 @ hotmail.com)
     * created in 2007/04/28 16:33:06
     */
    private static String bytesToHexStr(byte[] b) {
        if (b == null) return "";
        StringBuffer strBuffer = new StringBuffer(b.length * 3);
        for (int i = 0; i < b.length; i++) {
            String s = Integer.toHexString(b[i] & 0xff);
            if (s.length() == 1) {
                strBuffer.append("0");
            }
            strBuffer.append(s);
            strBuffer.append(" ");
        }
        return strBuffer.toString();
    }

    /**
     * 16进制的字符串表示转成字节数组
     *
     * @param hexString 16进制格式的字符串
     * @return 转换后的字节数组
     **/
    public static byte[] toByteArray(String hexString) {
        if (hexString == null || "".equals(hexString.trim()))
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase().replaceAll(" ", "");
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "°→\t\r\n";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            System.out.println("Unicode of '" + ch + "' is: \\u" + Integer.toHexString(ch));
        }
        System.out.println("----------------");
        str = "你";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            System.out.println("Unicode of '" + ch + "' is: \\u" + Integer.toHexString(ch));
            System.out.println(ch + " bytes:" + Arrays.toString(str.getBytes()));
        }
        System.out.println("----------------");
        System.out.println(str + " bytes:" + Arrays.toString(str.getBytes()));
        System.out.println(str + " byte len:" + str.getBytes().length);
        System.out.println(str + " str.length: " + str.length());

        System.out.println(Arrays.toString("您好".getBytes("GBK")));
        System.out.println(Arrays.toString("您好".getBytes("UTF-8")));
        System.out.println(Arrays.toString("您好".getBytes()));

        String sp = "31303639 30 34 36 38 20 393134313400000000000000";
        byte[] bs2 = toByteArray(sp);
        System.out.println(new String(bs2));

        String s = "14 07 1F 0E 39 0D";
        byte[] bs = toByteArray(s);
        String rs = "";
        for (byte b : bs) {
            rs += (int) b;
        }

        System.out.println(rs);

        String hs = Long.toHexString(20731145713L);
        System.out.println(hs);

        try {
            System.out.println("您好".getBytes("GBK").length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
